package com.group01.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group01.community.entity.*;
import com.group01.community.entity.vo.SysDeptVo;
import com.group01.community.entity.vo.SysMenuVo;
import com.group01.community.entity.vo.SysUserInfoVo;
import com.group01.community.mapper.SysDeptMapper;
import com.group01.community.mapper.SysDictTypeMapper;
import com.group01.community.service.SysDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author guoyuan
 * @since 2025-05-01
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 根据用户名查询三级菜单
     */
    @Override
    public List<SysDeptVo> getDeptsByName(String deptname) {
        return sysDeptMapper.getDeptsByName(deptname);
    }


    /**
     * 根据菜单名称获取菜单信息(树状)
     *
     * @param deptName
     * @return
     */
    @Override
    public SysDept getDeptByName(String deptName) {
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        wrapper.eq("dept_name", deptName);
        return sysDeptMapper.selectOne(wrapper);
    }

    /**
     * 查询所有分级菜单(四级菜单+模糊查询)
     *
     * @return
     */
    @Override
    public List<SysDeptVo> getDeptList( String deptName, String status) {
        List<SysDeptVo> deptList = sysDeptMapper.getDeptList(deptName, status);
        return deptList;
    }

    /**
     * 添加菜单
     *
     * @param sysDept
     * @return
     */
    @Override
    public void addDept(SysDept sysDept) {
        sysDeptMapper.insert(sysDept);
    }

    /**
     * 更新菜单
     *
     * @param sysDept
     * @return
     */
    @Override
    public void updateDept(SysDept sysDept) {
        sysDeptMapper.updateById(sysDept);
    }

    /**
     * 删除菜单
     *
     * @param deptId
     * @return
     */
    @Override
    public void deleteDept(Long deptId) {
        sysDeptMapper.deleteById(deptId);
    }


    /**
     * 根据角色id查询 岗位权限
     * @param roleId
     * @return
     */
    @Override
    public List<SysDept> getDeptListByRoleId(String roleId) {
        return sysDeptMapper.getDeptListByRoleId(roleId);
    }


    @Override
    public List<SysDept> getDeptByDeptName(String deptName) {
        return sysDeptMapper.getDeptByDeptName(deptName);
    }

    @Override
    public List<SysDeptVo> getDeptByDeptNameAndRole(SysUser sysUser ) {
        List<SysDeptVo> info = sysDeptMapper.getDeptByDeptNameAndRole(sysUser);
        ArrayList<SysDeptVo> sysDeptVo;
        sysDeptVo = (ArrayList<SysDeptVo>) info.stream().filter(a -> a.getParentId() == 0).collect(Collectors.toList());
        for (SysDeptVo dept : sysDeptVo) {
            dept.setDepts(info.stream().filter(dept2 -> dept2.getParentId().equals(dept.getDeptId())).collect(Collectors.toList()));
        }
        for (SysDeptVo dept : sysDeptVo) {
            List<SysDeptVo> dept3 = dept.getDepts();
            for (SysDeptVo city : dept3) {
                city.setDepts(info.stream().filter(town -> town.getParentId().equals(city.getDeptId())).collect(Collectors.toList()));
            }
        }
        return sysDeptVo;
    }

    @Override
    public List<SysDeptVo> getDeptLists(String deptName, String status) {
        return sysDeptMapper.getDeptLists(deptName,status);
    }
}

